<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>上传附件</title>
		<script src="js/jquery-1.9.0.min.js"></script>
	</head>

	<body>
		<form action="UploadFile.ashx" method="post" enctype="multipart/form-data">
			<input type="file" name="fileUpload" /><br />
			<input type="submit" value="上传文件" /><br />
		</form>
		<script>
			public void ProcessRequest(HttpContext context) {
				context.Response.ContentType = "text/html";
				HttpServerUtility server = context.Server;
				HttpRequest request = context.Request;
				HttpResponse response = context.Response;

				HttpPostedFile file = context.Request.Files[0];
				if(file.ContentLength > 0) {
					string extName = Path.GetExtension(file.FileName);
					string fileName = Guid.NewGuid().ToString();
					string fullName = fileName + extName;
					string imageFilter = ".jpg|.png|.gif|.ico"; // 随便模拟几个图片类型
					if(imageFilter.Contains(extName.ToLower())) {
						string phyFilePath = server.MapPath("~/Upload/Image/") + fullName;
						file.SaveAs(phyFilePath);
						response.Write("上传成功！文件名：" + fullName + "<br />");
						response.Write(string.Format("<img src='Upload/Image/{0}'/>", fullName));
					}
				}
			}
		</script>

	</body>

</html>